package com.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyin.entity.Favorites;
import com.douyin.entity.FavoritesVideo;
import com.douyin.exception.BaseException;
import com.douyin.mapper.FavoritesMapper;
import com.douyin.service.FavoritesVideoService;
import com.douyin.service.IFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements IFavoritesService {

    @Autowired
    private FavoritesVideoService favoritesVideoService;
    @Override
    public void exist(Long userId, Long defaultFavoritesId) {
        Long count = count(new LambdaQueryWrapper<Favorites>().eq(Favorites::getUserId, userId).eq(Favorites::getId,defaultFavoritesId ));
        if (count == 0){
            throw new BaseException("收藏夹选择错误");
        }
    }

    @Override
    public List<Favorites> getFavorites(Long userId) {
        final List<Favorites> list = this.list(new LambdaQueryWrapper<Favorites>().eq(Favorites::getUserId, userId));
        if (ObjectUtils.isEmpty(list)){
            return Collections.emptyList();
        }

        list.forEach(favorites -> {
            Long fId = favorites.getId();
            favorites.setVideoCount(favoritesVideoService.count(new LambdaQueryWrapper<FavoritesVideo>()
                    .eq(FavoritesVideo::getFavoritesId, fId).eq(FavoritesVideo::getUserId, userId)));
        });
        return list;
    }

    @Override
    public void remove(Long id, Long userId) {
        // 不能删除默认收藏夹
        final Favorites defaultFavorites  = this.getOne(
                new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, id).eq(Favorites::getUserId, userId));
        if (defaultFavorites.getName().equals("默认收藏夹")){
            throw new BaseException("不能删除默认收藏夹");
        }

        final boolean result = remove(new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, id).eq(Favorites::getUserId, userId));
        // 如果能删除成功说明是自己的收藏夹
        if (result){
            favoritesVideoService.remove(new LambdaQueryWrapper<FavoritesVideo>().eq(FavoritesVideo::getFavoritesId,id));
        }else {
            throw new BaseException("不可删除别人的收藏夹");
        }

    }

    @Override
    public boolean favorites(Long fId, Long vId, Long userId) {
        try{
            FavoritesVideo favoritesVideo = new FavoritesVideo();
            favoritesVideo.setFavoritesId(fId);
            favoritesVideo.setVideoId(vId);
            favoritesVideo.setUserId(userId);
            favoritesVideoService.save(favoritesVideo);
        }catch (Exception e){
            favoritesVideoService.remove(new LambdaQueryWrapper<FavoritesVideo>()
                    .eq(FavoritesVideo::getFavoritesId, fId).eq(FavoritesVideo::getVideoId, vId).eq(FavoritesVideo::getUserId, userId));
            return false;
        }
        return true;
    }

    @Override
    public List<Long> listVideoIdByFavoritesId(Long favoritesId, Long userId) {
        final Favorites favorites = getOne(new LambdaQueryWrapper<Favorites>().eq(Favorites::getId, favoritesId).eq(Favorites::getUserId, userId));
        if (favorites == null){
            throw new BaseException("收藏夹为空");
        }
        return favoritesVideoService.list(new LambdaQueryWrapper<FavoritesVideo>()
                .eq(FavoritesVideo::getFavoritesId, favoritesId).eq(FavoritesVideo::getUserId, userId))
                .stream().map(FavoritesVideo::getVideoId).collect(Collectors.toList());
    }
}
